package com.example.zhaohang.grildlayoutandview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private GridLayout mGridLayout;
    private List<MyView> mMyViews = new ArrayList<>();
    private List<TextView> mTextViews = new ArrayList<>();
    private List<ImageView> mImageViews = new ArrayList<>();
    private List<LinearLayout> mLinearLayouts = new ArrayList<>();
    private MyView mView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGridLayout = (GridLayout) findViewById(R.id.gridlayout);


        //分别把不同的控件进行分别集合添加
        for (int i = 0; i < 3; i++) {
            mView = new MyView(MainActivity.this);
            if (i == 1) {
                mView.getLinearLayout().setSelected(true);
                mView.getTextView().setText("第几名了");
            }
            mMyViews.add(mView);
            mTextViews.add(mView.getTextView());
            mImageViews.add(mView.getImageView());
            mLinearLayouts.add(mView.getLinearLayout());

        }
        //用集合的方式进行各个view设置点击事件
        for (int i = 0; i < mLinearLayouts.size(); i++) {
            final int b = i;
            mGridLayout.addView(mMyViews.get(i));
            mImageViews.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mGridLayout.removeView(mMyViews.get(b));
                }
            });
            mLinearLayouts.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int j = 0; j < mLinearLayouts.size(); j++) {
                        mLinearLayouts.get(j).setSelected(false);
                    }
                    mLinearLayouts.get(b).setSelected(true);

                }
            });
        }



    }

    public void addview(View view) {
        mView = new MyView(MainActivity.this);
        mMyViews.add(mView);
        mTextViews.add(mView.getTextView());
        mImageViews.add(mView.getImageView());
        mLinearLayouts.add(mView.getLinearLayout());

        mGridLayout.addView(mView);
        //用集合的方式进行各个view设置点击事件
        for (int i = 0; i < mLinearLayouts.size(); i++) {
            final int b = i;
            mImageViews.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mGridLayout.removeView(mMyViews.get(b));
                }
            });
            mLinearLayouts.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (int j = 0; j < mLinearLayouts.size(); j++) {
                        mLinearLayouts.get(j).setSelected(false);
                    }
                    mLinearLayouts.get(b).setSelected(true);
                }
            });
        }

    }
}
